<script type="text/javascript">
	function checkSubmit(){
		if(document.cominfo.name.value == ''){
			showAlert('Thông báo', 'Vui lòng nhập tên doanh nghiệp', 'name');
			return false;
		}
		if(document.cominfo.addr.value == ''){
			showAlert('Thông báo', 'Vui lòng nhập địa chỉ doanh nghiệp', 'addr');
			return false;
		}
		if(document.cominfo.hotline.value == ''){
			showAlert('Thông báo', 'Vui lòng nhập hot-line', 'hotline');
			return false;
		}
		if(document.cominfo.email.value == ''){
			showAlert('Thông báo', 'Vui lòng nhập email doanh nghiệp', 'email');
			return false;
		}
		if(!checkEmailAddr(document.cominfo.email.value)){
			showAlert('Thông báo', 'Địa chỉ email không hợp lệ, vui lòng kiểm tra lại', 'email');
			return false;
		}
		if(document.cominfo.yahoo.value == ''){
			showAlert('Thông báo', 'Vui lòng nhập nick yahoo hỗ trợ trực tuyến', 'yahoo');
			return false;
		}
		return true;
	}
</script>
<?php
	@$name    =    $_POST['name'];
	@$addr    =    $_POST['addr'];
	@$hotline = $_POST['hotline'];
	@$email   =   $_POST['email'];
	@$yahoo   =   $_POST['yahoo'];
	@$footer  =  $_POST['footer'];
	@$contact = $_POST['contact'];
	
	if(isset($name) && isset($addr) && isset($hotline) && isset($email) && isset($yahoo))
	{
		$esc_val = $exec_mysql->escape_string(array($name, $addr, $hotline, $email, $yahoo, $footer));
		$query = sprintf("update extra set val = '%s' where ref = 'cty_name'", $esc_val[0]);
		$exec_mysql->execute($query, 0);
		$query = sprintf("update extra set val = '%s' where ref = 'cty_address'", $esc_val[1]);
		$exec_mysql->execute($query, 0);
		$query = sprintf("update extra set val = '%s' where ref = 'hot_line'", $esc_val[2]);
		$exec_mysql->execute($query, 0);
		$query = sprintf("update extra set val = '%s' where ref = 'email'", $esc_val[3]);
		$exec_mysql->execute($query, 0);
		$query = sprintf("update extra set val = '%s' where ref = 'yahoo'", $esc_val[4]);
		$exec_mysql->execute($query, 0);
		$query = sprintf("update extra set val = '%s' where ref = 'footer'", $esc_val[5]);
		$exec_mysql->execute($query, 0);
		$query = sprintf("update static_page set content = '%s' where ref = 'contact'", $contact);
		$exec_mysql->execute($query, 0);
		echo '<script type="text/javascript">showAlert(\'Thông báo\', \'Cập nhật thành công\', \'\');</script>';
	}

	$name = $exec_mysql->execute("select val from extra where ref = 'cty_name'", 1);
	$addr = $exec_mysql->execute("select val from extra where ref = 'cty_address'", 1);
	$hotline = $exec_mysql->execute("select val from extra where ref = 'hot_line'", 1);
	$email = $exec_mysql->execute("select val from extra where ref = 'email'", 1);
	$yahoo = $exec_mysql->execute("select val from extra where ref = 'yahoo'", 1);
	$footer = $exec_mysql->execute("select val from extra where ref = 'footer'", 1);
	$contact = $exec_mysql->execute("select content from static_page where ref = 'contact'", 1);
?>
<form method="post" onsubmit="return checkSubmit()" name="cominfo">
	<p class="group-line">
        <span class="group-title">Thông tin doanh nghiệp</span>
    </p>
    <table class="form-layout">
    	<tr>
        	<td width="150">Tên doanh nghiệp*</td>
            <td><input type="text" name="name" id="name" style="width:400px" value="<?php echo htmlspecialchars($name)?>" /></td>
        </tr>
        <tr>
        	<td>Địa chỉ*</td>
            <td><input type="text" name="addr" id="addr" style="width:400px" value="<?php echo htmlspecialchars($addr)?>" /></td>
        </tr>
        <tr>
        	<td>Hot-line*</td>
            <td><input type="text" name="hotline" id="hotline" style="width:400px" value="<?php echo htmlspecialchars($hotline)?>" /></td>
        </tr>
        <tr>
        	<td>Email*</td>
            <td><input type="text" name="email" id="email" style="width:400px" value="<?php echo htmlspecialchars($email)?>" /></td>
        </tr>
        <tr>
        	<td>Yahoo*</td>
            <td><input type="text" name="yahoo" id="yahoo" style="width:400px" value="<?php echo htmlspecialchars($yahoo)?>" /></td>
        </tr>
    </table>
    <p class="group-line">
        <span class="group-title">Thông tin cuối trang</span>
    </p>
   	<table class="form-layout">
        <tr>
        	<td width="150">Footer</td>
            <td><input type="text" name="footer" id="footer" style="width:400px" value="<?php echo htmlspecialchars($footer)?>" /></td>
        </tr>
    </table>
    <p class="group-line">
        <span class="group-title">Thông tin liên hệ</span>
    </p>
   	<table class="form-layout">
        <tr>
        	<td width="150"></td>
            <td>
            	<textarea id="editor" name="contact" ><?php echo $contact; ?></textarea>
                <script>
					CKEDITOR.replace('editor',{
						enterMode: CKEDITOR.ENTER_BR
					});
					CKEDITOR.on('instanceReady',function() {
						adjustSize();
					});
				</script>
            </td>
        </tr>
    </table>
    <p class="footer-form">
    	<input type="submit" value="Cập nhật" />
        <input type="reset" value="Undo" />
    </p>
</form>